<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsNews extends BgcmsProcess{
	
	var $db;
 	var $smarty;
 		
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
		
	public function __destruct() {
		//
	}
	
	public function process() {
		if(isset($_POST['act']) && $_POST['act'] !="") {
			$act = $_POST['act'];
			if($act == 'delnews')
				$this->delete();
			elseif($act == 'createnews')
				$this->save();	
			elseif($act == 'updatenews')
				$this->update();		
	    } else {
			if($_GET['sub']!="" && $_GET['sub'] == 'editnews'){
				$newsData = BgcmsNewsBean::get_news_by_id($this->db, $_GET['id']);
				$this->smarty->assign("ARR_NEWS", $newsData);
			}
	    }
		$this->show_news_list();

   	}
   	public function show_news_list() {
		//create new array of News
	    $newsData = array();
		$countnews = BgcmsNewsBean::count_all_news($this->db);
		$newsData['data'] = BgcmsNewsBean::get_all_news_paging($this->db,ADMINPAGING,"list");
		$result = BgcmsPaging::do_paging($countnews,$newsData, ADMINPAGING, "?module=BgcmsNews","list");
		$this->smarty->assign("ARR_ALL_NEWS_DATA", $result);
		$this->Run();
	}
	//delete News
	public function delete() {
		if(isset($_POST['id']) && $_POST['id'] !=""){
			$lastnews = BgcmsNewsBean::get_news_by_id($this->db, $_POST['id']);
			BgcmsNewsBean::delete($_POST['id']);
			@unlink('../'.$lastnews->news_image_small);
			@unlink('../'.$lastnews->news_image_large);
			$newsData = array();	
			$newsData['OK'] = 1;
			$newsData['message'] = ("News : ".$_POST['id']."  has been deleted."); 
			echo array2json($newsData);
		}		
	}
	
	//save News
	public function save() {
		$newsData = array();
		$newsData['news_name'] = $_POST['news_name'];
		$newsData['news_description'] = $_POST['news_description'];
		$newsData['creator_id'] = $_SESSION['user_id'];
		$newsData['active'] = $_POST['active'];
		$newsData['position'] = BgcmsNewsBean::count_all_news($this->db) +1;
		
		if($_FILES['image']['name'] != null && $_FILES['image']['name'] != "") {
			$upload = new BgcmsUpload($this->smarty);
			$image = $_FILES['image']['name'];
			$path = pathinfo($image);
			$image_type = strtolower($path['extension']);
			$namefile = explode(".".$image_type,$_FILES['image']['name']);
			if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
				$imagename = $upload->upload_news_thumbnail($_FILES['image']['tmp_name'],$namefile['0'], $image_type,"../".NEWS_UPLOAD_PATH_SMALL,"../".NEWS_UPLOAD_PATH_LARGE);
				$path = $imagename;
			} else {
				$_SESSION['file_error'] = $this->l['error']['imageError'];
			}

		}
		$newsData['news_image_small'] = NEWS_UPLOAD_PATH_SMALL.$path;	
		$newsData['news_image_large'] = NEWS_UPLOAD_PATH_LARGE.$path;	
		if($newsData['news_name']!="") {
			BgcmsNewsBean::save($this->db, $newsData);
			$message = ("News has been added."); 
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsNews");
		} else {
			$message = ("Please enter."); 
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsNews&sub=CreateNews");
		}
    }
	
	//update News
	public function update() {
		$newsData = array();
		$newsData['news_id'] = $_POST['news_id'];
		$newsData['news_name'] = $_POST['news_name'];
		$newsData['news_description'] = $_POST['news_description'];
		$newsData['active'] = $_POST['active'];
		
		$lastnews = BgcmsNewsBean::get_news_by_id($this->db, $newsData['news_id']);
		$newsData['news_image_small'] = $lastnews->news_image_small;	
		$newsData['news_image_large'] = $lastnews->news_image_large;
		if($_FILES['image']['name'] != null && $_FILES['image']['name'] != "") {
				$upload = new BgcmsUpload($this->smarty);
				$image = $_FILES['image']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				$namefile = explode(".".$image_type,$_FILES['image']['name']);
				if($image_type == "jpeg" || $image_type == "jpe" || $image_type == "jpg" || $image_type == "gif" || $image_type == "png" || $image_type == "bmp") {
					$imagename = $upload->upload_news_thumbnail($_FILES['image']['tmp_name'],$namefile['0'], $image_type,"../".NEWS_UPLOAD_PATH_SMALL,"../".NEWS_UPLOAD_PATH_LARGE);
					$path = $imagename;
					if($lastnews['news_image_small']!=""){
						@unlink('../'.$lastnews['news_image_small']);
						@unlink('../'.$lastnews['news_image_large']);
					}
					$newsData['news_image_small'] = NEWS_UPLOAD_PATH_SMALL.$path;	
					$newsData['news_image_large'] = NEWS_UPLOAD_PATH_LARGE.$path;
				} else {
					$_SESSION['file_error'] = $this->l['error']['imageError'];
				}
			}
		
		if($newsData['news_name']!="") {
			BgcmsNewsBean::update($newsData);
			$message = ("News has been updated."); 
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsNews");
		} else {
			$message = ("Please enter."); 
			setcookie("message", $message,time()+5);
			header("Location: ./?module=BgcmsNews&sub=editnews&id=".$newsData['news_id']);
		}
    }
	
}
?>